package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.RoomDeposit;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 房间押金
 * 
 * @param <list>
 */
@Component
public class RoomDepositDao<list> implements BaseDao<RoomDeposit> {

    @Override
    public int insert(RoomDeposit entity) {
        String sql =
            "insert into room_deposit (customer_id, room_id, employee_id_income, price,cashpay, is_return,room_real_order_id) "
                + "values ((select MAX(id) from customer), ?, ?, ?, ?, 0,(select max(id) from room_real_order));";
        return DBHelper.executeUpdate(sql, entity.getRoom_id(), entity.getEmployee_id_income(), entity.getPrice(),
            entity.getCashpay());
    }

    @Override
    public int update(RoomDeposit entity) {
        String sql = "update room_deposit set customer_id=?,room_real_order_id=?, room_id=?,price=?, where id=? ";
        return DBHelper.executeUpdate(sql, entity.getCustimer_id(), entity.getRoom_real_order_id(), entity.getRoom_id(),
            entity.getPrice(), entity.getId());
    }

    @Override
    public int delete(RoomDeposit entity) {
        String sql = "update room_deposit set is_deleted = 1 where id=?";
        return DBHelper.executeUpdate(sql, entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        String sql = "select  * from room_deposit where is_deleted = 0 ";

        return DBHelper.executeQuery(sql);
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        RoomDeposit roomDeposit = (RoomDeposit)obj[0];
        String sql =
            "select rd.id,c.realname cname ,rro.ordernumber rronumber,r.name rname,rd.price price,rro.gmt_create rrogmt, e.realname iname,ee.realname ename,rd.is_return ir from \n"
                + "room_deposit rd LEFT join customer c on rd.customer_id = c.id \n"
                + "LEFT  join room_real_order rro on rd.room_real_order_id = rro.id \n"
                + "LEFT  join room r on rd.room_id =  r.id \n"
                + "LEFT  join employee e on rd.employee_id_income  = e.id \n"
                + "LEFT JOIN employee ee on rd.employee_id_expend = ee.id\n" + "where 1=1 ";
        if (obj[1] != null && obj[2] != null) {
            sql += " and rro.gmt_create BETWEEN '" + obj[1] + "' AND '" + obj[2] + "' ";
        }
        sql += " and rd.is_return = " + roomDeposit.getIs_return();
        // if (roomDeposit.getIs_return() != 0) {
        // sql += " and rd.is_return = " + roomDeposit.getIs_return();
        // } else {
        // sql += " and rd.is_return != 1";
        // }
        if (roomDeposit.getCname() != null && !"".equals(roomDeposit.getCname())) {
            sql += " and concat(c.realname,r.name) like '%" + roomDeposit.getCname() + "%'";
        }
        return DBHelper.executeQuery(sql + " limit ?,?", page.getBegin(), page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(RoomDeposit entity) {
        String sql = "select * from room_deposit where id = ? and is_deleted = 0";
        return DBHelper.executeQuery(sql, entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        RoomDeposit roomDeposit = (RoomDeposit)obj[0];
        String sql = "select count(*) counts from \n"
            + "room_deposit rd LEFT join customer c on rd.customer_id = c.id \n"
            + "LEFT  join room_real_order rro on rd.room_real_order_id = rro.id \n"
            + "LEFT  join room r on rd.room_id =  r.id \n" + "LEFT  join employee e on rd.employee_id_income  = e.id \n"
            + "LEFT JOIN employee ee on rd.employee_id_expend = ee.id\n" + "where 1=1 ";
        if (obj[1] != null && obj[2] != null) {
            sql += " and rro.gmt_create BETWEEN '" + obj[1] + "' AND '" + obj[2] + "' ";
        }
        sql += " and rd.is_return = " + roomDeposit.getIs_return();
        // if (roomDeposit.getIs_return() != 0) {
        // sql += " and rd.is_return = " + roomDeposit.getIs_return();
        // } else {
        // sql += " and rd.is_return != 9";
        // }
        if (roomDeposit.getCname() != null && !"".equals(roomDeposit.getCname())) {
            sql += " and concat(c.realname,r.name) like '%" + roomDeposit.getCname() + "%'";
        }

        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }

    public int addMaxId() {
        int id = Integer
            .valueOf(DBHelper.executeQuery("select MAX(id) maxid from room_deposit").get(0).get("maxid").toString());
        String sql =
            "update room_deposit set room_real_order_id = (select MAX(id) from room_real_order) where id=(select max(id) from room_real_order)";
        return DBHelper.executeUpdate(sql, id);
    }

    /**
     * @Author: 陈世纪
     * @Date: 23:44 2020/3/9
     * @Description: 酒店前台-账户管理--后台
     */
    public List<Map<String, Object>> check_outRoom() {
        String sql =
            "select * from room_real_order rro " + "right join room_deposit rd on rro.room_deposit_id = rd.id  "
                + "left join room_real_order_details rrod on rro.id = rrod.room_real_order_id  "
                + "where rd.is_return = 1 and rd.is_deleted = 0 and rrod.is_deleted = 0 " + "GROUP BY rd.id";
        return null;
    }

    public int returnDepositById(Integer id) {
        return DBHelper.executeUpdate("update room_deposit set is_return=1 where id=?", id);
    }
}
